// pages/find/index.js
import wxRequest from '../../utils/wxRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pages:0,//总页数
    openMenu: 0,
    tabbar: [],
    datalist: [],
    obj:{//获取文章列表的参数对象
      page:1,
      number:15
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取文章分类
    wxRequest.postRequest('/m/user/typelist', { modilar: 0, pid: 0 }).then(res => {
      this.setData({
        tabbar: res.data
      })
    }).catch(error => {
      console.log(error, 'error');
    });
    this.getData(this.data.obj);
  },

  openTabMenu(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      openMenu: index
    })
  },
  
  // 点击分类获取相关文章列表
  selectedType(e) {
    this.setData({
      openMenu: 0,
      datalist:[],
      obj: {
        modular: e.currentTarget.dataset.pid,
        father_id: e.currentTarget.dataset.pid,
        second_id: e.currentTarget.dataset.id,
        page:1   //按分类重新获取
      }
    })
    this.getData(this.data.obj);

  },
  // 获取文章列表
  getData: function (obj) {
    wx.showLoading();
    wxRequest.getRequest('/m/news/index', obj).then(res => {
      const totalList = this.data.datalist.concat(res.data.newsItem);
      this.setData({
        datalist: totalList,
        pages: res.data.pages
      });
      wx.hideLoading();
    }).catch(error => {
      console.log(error, 'error');
      wx.hideNavigationBarLoading();
    })
  },

  // 跳转详情页面
  onDetailTap: function (e) {
    const id = e.currentTarget.dataset.id;
     wx.navigateTo({
      url: '../articles/detail/index?id='+id
    })
  },

  // 下拉加载
  toLower(e){
    this.setData({ obj:{ page: this.data.obj.page + 1 } });
    if(this.data.obj.page<=this.data.pages){
      this.getData(this.data.obj);
    }
    
  }
})